package cn.toolck.my.spring.utils;

import cn.toolck.my.spring.annotation.Component;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @ClassName ConnectionUtils
 * @Description
 * @Author Toolck Rin
 * @Date 2021/2/4 17:35
 * @Version 1.0
 **/
@Component("connectionUtils")
public class ConnectionUtils {
    private ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal();

    public Connection getConnection() throws SQLException {
        Connection connection = connectionThreadLocal.get();
        if (connection == null) {
            connection = DruidUtils.getInstance().getConnection();
            connectionThreadLocal.set(connection);
        }
        return connection;
    }

}
